package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-02-24
 */
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private IBrandService brandService;

    /**
     * 获取品牌名称
     * @return
     * TableDataInfo 表格分页数据对象
     */
    //为品牌添加请求权限
    @RequiresPermissions("product:brand:list")
    @GetMapping("/list")
    @Operation(summary = "查询品牌列表")
    //public TableDataInfo list(Brand brand) {
    public TableDataInfo list(@Parameter(description = "品牌名字") String name) {
        //分页参数封装了BaseController类
        startPage(); //若依自己封装的分页
        List<Brand> list = brandService.selectBrandList(name);
        return getDataTable(list);
    }
    //新增
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("product:brand:add")
    @Operation(summary="新增品牌")
    @PostMapping
    public AjaxResult add(@Parameter(description = "新增品牌对象") @RequestBody Brand brand){
        //设置当前用户
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.save(brand));
    }
    //修改
    @Log(title = "品牌管理",businessType = BusinessType.UPDATE)
    @RequiresPermissions("product:brand:edit")
    @Operation(summary = "修改品牌")
    @PutMapping
    public AjaxResult edit(@Parameter(description = "修改品牌对象") @RequestBody Brand brand){
        //设置当前对象
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateById(brand));

    }
    //删除
    @Log(title="品牌管理",businessType = BusinessType.DELETE)
    @RequiresPermissions("product:brand:remove")
    @Operation(summary = "删除品牌")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(description = "品牌id列表",example = "[1,2]")
                                 @PathVariable List<Long> ids){
        return toAjax(brandService.removeByIds(ids));
    }
    @Log(title = "测试",businessType = BusinessType.TEST)
    @Operation(summary = "测试")
    @DeleteMapping("/test")
    public AjaxResult test(){
        return success();
    }
}
